$(document).ready(function ()
{
    //retrieve all the previewboxes

    $("div[target='previewBox']").each(function (a, b)
    {
        var fileUrl = $(this).attr('fileUrl');
        var fileExtension = fileUrl.substring(fileUrl.length - 3, fileUrl.length);

        var height = $(this).attr('previewheight');
        var width = $(this).attr('previewwidth');

        var id = $(this).attr('id');

        //display the preview in a modal dialog or inline?
        var asPreviewDialog = $(this).attr('DialogPreview') == '1';

        if (fileExtension == 'pdf')
        {
            $(this).html('');
            var pdf = new PDFObject({
                url: fileUrl,
                pdfOpenParams: {
                    pagemode: 'thumbs',
                    scrollbars: $(this).attr('scrollbars'),
                    toolbar: $(this).attr('toolbars'),
                    statusbar: $(this).attr('statusbar'),
                    messages: $(this).attr('documentbar'),
                    navpanes: $(this).attr('navigationpane')
                }
            });

            if (asPreviewDialog)
            {
                //insert another div
                var dialogDivId = id + '-dialog';
                var previewId = id + '-preview';

                $("<div title='Document preview' id='" + dialogDivId + "'></div>").appendTo($(this));

                $("<a style='cursor:pointer' id='" + previewId + "'>Preview</a>").appendTo($(this));
                $("#" + previewId).click(function (a, b)
                {
                    pdf.embed(dialogDivId);
                    $("#" + dialogDivId).dialog({ height: height, width: width });
                });
            }
            else
            {
                $(this).height(height).width(width);
                pdf.embed(id);
            }

            $(this).show();
        }
        else
        {
            $(this).hide();
        }

    });
});